#include "Entity.h"


Entity::~Entity()
{
	DestroyAllComponents();
}
void Entity::RegisterComponent( HashedString name, Pointer<IComponent>& component )
{
	m_components.insert(IComponent::Pair_t( name, component ));
}
void Entity::UnregisterComponent( HashedString name )
{
	IComponent::Map_t::iterator it = m_components.find(name);
	if (it != m_components.end())
		m_components.erase(name);
}
void Entity::DestroyAllComponents()
{
	for (IComponent::Map_t::iterator it = m_components.begin(); it != m_components.end(); ++it)
	{
		it->second->Remove();
	}
	m_components.clear();
}
IComponent* Entity::FindComponent( HashedString string )
{
	IComponent::Map_t::iterator it = m_components.find(string);
	if ( it != m_components.end() )
	{
		return (it->second);
	}
	return 0;
}
Pointer<IComponent>& Entity::FindComponentSmtPtr( HashedString string )
{
	static Pointer<IComponent> nil(0);
	IComponent::Map_t::iterator it = m_components.find(string);
	if ( it != m_components.end() )
	{
		return (it->second);
	}
	return nil;
}

void Entity::ReceiveMessage( IMessage* message )
{
	for (IComponent::Map_t::iterator it = m_components.begin(); it != m_components.end(); ++it)
	{
		IComponent* component = (it->second);
		component->ReceiveMessage(message);
	}
}
IComponent::Map_t& Entity::Components()
{
	return m_components;
}